<?php 

class Db {

	private $host = null ; 
	private $name = null ; 
	private $pass = null ;  
    private $port = '3306' ; 
    private $db = null ; 

    private $conn ; 

    private $sql ; 


	function __construct(){

		$db  = C('DB');

		$this->host = $db['DB_HOST'];
		$this->name = $db['DB_USER'];
		$this->pass = $db['DB_PASS'];
		$this->port = $db['DB_PORT'];
		$this->db = $db['DB_DATABASE'];
		if(!$this->conn()){
			Throw new Exception('数据库未正常连接'); 
		}
		$this->sql = '';
	}

	private function conn(){
		$this->conn = mysql_connect($this->host . ':' . $this->port ,
					$this->name , $this->pass 
			);
		if($this->conn){
			mysql_query('set names utf8');
		    mysql_select_db($this->db);
			return true;
		}else{
			return false  ;
		}
	}

	public function select($field = ''){
		if(empty($field)){
			$this->sql .= "SELECT * FROM %s " ;
		}else{
			$this->sql .= "SELECT " . $field . " FROM %s";
		}
		return $this;
	}

	public function table($tablename){
		if(empty($tablename)){
			Throw new Exception("表名未定义");
		}else{
			$this->table = $tablename;
		}
		return $this;
	}

	public function order($order = array('id asc')){
		if(is_string($order)){
			$this->sql .= "ORDER BY " .  $order . " " ;
		}elseif(is_array($order)){
			$this->sql .="ORDER BY ";

			foreach ($order as $key => $value) {
				$this->sql .= $key . " " . $value . " ";
			}
		}
		return $this;
	}

	public function limit($start = 0 , $offset = null){

		if($offset === null){
			$this->sql .= "LIMIT " . $start . " " ; 
		}else{
			$this->sql .= "LIMIT " . $start . "," . $offset . " ";
		}
		return $this;
	}

	public function add($data = array()){

		if(is_array($data)){

			$sql = "INSERT INTO %s SET ";

			foreach($data as $key => $val){
				$sql .= $key . ' = ' . htmlspecialchars($val) . ',' ; 
			}

			$this->sql = substr($sql , 0 , -1) . ' '; 
			return $this;
		}
	}

	public function update($data){

		$sql = "UPDATE %s SET " ;  
		foreach($data as $key => $val){
			$sql .= $key . ' = ' . htmlspecialchars($val) . ',' ; 
		}

		$this->sql = substr($sql , 0 , -1) . ' ' ; 
		return $this ; 
	}

	public function delete($data){
		$sql = "DELETE FROM %s " ;
		$this->sql = $sql ;
		return $this;
	}

	public function where($map = array()){
		if(is_array($map)){
			$sql = "WHERE " ;

			foreach($map as $key => $val){
				$sql .= $key . '=' . htmlspecialchars($val) . ' AND ' ;
			}
			$this->sql = substr($sql, 0 , -4) ;
		}
	}


	public function execsql(){

		$this->sql = sprintf($this->sql  , $this->table);
		try {
			if(preg_match('/^SELECT/i', $this->sql)){

				$res = mysql_query($this->sql , $this->conn);

				if($res){
						$data = array() ;
						while($temp = mysql_fetch_assoc($res)){
							$data [] = $temp ; 
						}
						return $data ;
				}else{
					return mysql_errno();
				}
			}else{
				return mysql_query($this->sql);
			}

		} catch (Exception $e) {
				echo $e ; 
		}
		return $this;

	}

	function query($sql){
		$this->sql = $sql ;
		return $this->execsql($sql);
	}

	function doquery($sql){
		return mysql_query($sql);
	}

	function getSql(){
		return $this->sql;
	}



}